package repositories

import (
	"errors"
	"github.com/sirupsen/logrus"
	"log"
	"micro-member/datamodels"
	"micro-member/initialize"
	"micro-member/libs"
	"strconv"
	"time"
)

type UserRepoInterface interface {
	Insert(user datamodels.User) (int64, error) //插入注册用户
	CheckUserLoginInfo(nickname string, password string) (datamodels.User, error) //检查登录信息
	SelectInfo(id int64) (datamodels.User, error) //获得用户信息
	UpdateUserInfo(params map[string]interface{}, value map[string]interface{},num int64,typo string,remark string) (int64, error) //更新用户信息
}

//	返回结构体对象
func NewUserRepoInterface() UserRepoInterface {
	return &userRepo{}
}

//	userRepo构体
type userRepo struct {
}


//插入注册用户
func (this *userRepo) Insert(userinfo datamodels.User) (int64, error) {
	//用户奖励
	var record datamodels.RewardRecord
	//用户录表
	var info datamodels.User

	tx := initialize.MsqlDb.Begin()
	if err := tx.Model(&info).Where("nickname = ?", userinfo.Nickname).Find(&info).Error;err != nil {
		logrus.Infof("[用户注册-查询异常]-[%s]", err)
	}
	if info.ID > 0 {
		tx.Rollback()
		return 0, errors.New("用户昵称已存在")
	}
	//插入数据库
	if err := tx.Model(&info).Create(&userinfo).Error; err != nil {
		logrus.Errorf("[用户注册-插入失败]-[%s]", err)
		tx.Rollback()
		return 0, err
	}

	//消费记录
	record.ID = 0
	record.Uid = userinfo.ID
	record.Type = "reward"
	record.Status = 1
	record.CreateTime = time.Now().Unix()
	record.UpdateTime = time.Now().Unix()
	record.Num = int64(userinfo.Reward)
	record.Remark = "新用户注册奖励飞吻"
	if err := tx.Create(&record).Error; err != nil {
		log.Println("[消费流水-创建消费流水]-[%s]", err.Error())
		tx.Rollback()
		return 0, err
	}

	return userinfo.ID,tx.Commit().Error
}

//检查登录信息
func (this *userRepo) CheckUserLoginInfo(nickname string, password string) (datamodels.User, error) {
	var userInfo datamodels.User
	//读取数据库
	if err := initialize.MsqlDb.Where("nickname = ?", nickname).Find(&userInfo).Error; err != nil {
		log.Println("[查找用户信息失败]-[%s]", nickname, err.Error())
		return userInfo, err
	}
	//效验验证密码
	genPassword := libs.GenSaltPassword(password, strconv.FormatInt(userInfo.CreateTime,10))//已用户注册时间为盐值
	if genPassword == userInfo.Password {
		return userInfo, nil
	}else{
		var emptyUser datamodels.User
		return emptyUser, errors.New("密码验证失败")
	}
}

//获得用户信息
func (this *userRepo) SelectInfo(id int64) (datamodels.User, error) {
	var userInfo datamodels.User
	//redis礼物key
	//jmfMemberKey := ReturnRedisKey(API_CACHE_JMF_MEMBER, userId)
	//result, err := initialize.RedisCluster.Get(jmfMemberKey).Bytes()

	//读取数据库
	if err := initialize.MsqlDb.Where("id = ? ", id).Find(&userInfo).Error; err != nil {
		log.Println("[获取用户信息失败]-[%s]", err.Error())
		return userInfo, err
	}
	return userInfo, nil
}

//更新用户信息
func (this *userRepo) UpdateUserInfo(params map[string]interface{}, value map[string]interface{},num int64,typo string,remark string) (int64,error) {
	var record datamodels.RewardRecord
	//事务开始
	tx := initialize.MsqlDb.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err := tx.Error; err != nil {
		log.Println("UpdateUserInfo:",err)
		return 0, err
	}
	if err := tx.Model(&datamodels.User{}).Where(params).Updates(value).Error; err != nil {
		tx.Rollback()
		log.Println("[更新用户信息失败]-[%s]", err.Error())
		return 0,err
	}

	//消费记录
	record.ID = 0
	record.Uid = params["id"].(int64)
	record.Type = typo
	record.CreateTime = time.Now().Unix()
	record.UpdateTime = time.Now().Unix()
	record.Num = num
	record.Remark = remark
	if err := tx.Create(&record).Error; err != nil {
		log.Println("[消费流水-创建消费流水]-[%s]", err.Error())
		tx.Rollback()
		return 0, err
	}

	return record.ID, tx.Commit().Error
}